;
var PLATFORM = PLATFORM || {};

// popup显示
PLATFORM.popupShow = function(dom) {
    $(dom).show();
};
// popup隐藏
PLATFORM.popupClose = function() {
    $(".u-popup .btn-close").click(function() {
        $(this).parents(".u-popup").hide()
    });
};

PLATFORM.pageTimeout = function() {
        $(document).ajaxSuccess(function(e, xhr, settings, data) {
            if (data != null && data.idsIntercepted == true) {
                PLATFORM.headTip("页面登录超时，请重新登录");
            }
        });
    }
    // 日期选择 年-月-日 时:分:秒
PLATFORM.timepickerRange = function(from, to) {
    $(from).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $(to).datepicker("option", "minDate", selectedDate);
        }
    });
    $(to).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $(from).datepicker("option", "maxDate", selectedDate);
        }
    });
};
// 日期选择 年-月-日
PLATFORM.datepickerRange = function(from, to) {
    $(from).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            $(to).datepicker("option", "minDate", selectedDate);
        }
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            $(from).datepicker("option", "maxDate", selectedDate);
        }
    });
};
/*
 * 对最小时间/最大时间做限制
 * count: 1最小时间明天，-1昨天，0今天
 */
PLATFORM.datepickerRangelimit = function(from, mincount, to) {
    $(from).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            if(getMinDate(0) > selectedDate){
                $(to).datepicker("option", "minDate", getMinDate(0));    
            }else{
                $(to).datepicker("option", "minDate", selectedDate);
            }
            var mm = new Date(selectedDate).getMonth();
            $(to).datepicker("option", "maxDate", getLastDay(mm));
        },
        minDate: getFirstDay()
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd",
//      onClose: function(selectedDate) {
//          $(from).datepicker("option", "maxDate", selectedDate);
//      },
        maxDate: getLastDay(),
        minDate: getMinDate(0)
    });
};

PLATFORM.showTips = function(type, msg) {
    $("#popup_tips").show().find(".container p").text(msg);
    var tipicon = $("#popup_tips").find(".container div");
    if (type == "success") {
        tipicon.removeClass("error").removeClass("warning").addClass("success");
    } else if (type == "error") {
        tipicon.removeClass("success").removeClass("warning").addClass("error");
    } else if (type == "warning") {
        tipicon.removeClass("error").removeClass("success").addClass("warning");
    }
};

function getMinDate(count) {
    var dd = new Date();
    dd.setDate(dd.getDate() + count);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份的日期 
    var d = dd.getDate();
    return y + "-" + (m>10?m:'0'+m) + "-" + (d>10?d:'0'+d);
}
function getFirstDay() {
    var dd = new Date();
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份
    var d = "1";
    return y + "-" + (m>10?m:'0'+m) + "-" + (d>10?d:'0'+d);
}
function getLastDay(mm){
    var dd = new Date();
    var y = dd.getFullYear();
    var m = parseInt(mm==undefined?dd.getMonth():mm,10) + 1; //获取当前月份
    var day = new Date(y,m,0);   
    var d = day.getDate();
    return y + "-" + (m>10?m:'0'+m) + "-" + (d>10?d:'0'+d);
}
$(function() {
    PLATFORM.popupClose();
    //PLATFORM.pageTimeout();
});
